const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const utils = require('../../../utils');
const moment = require('moment');

class SalesDownloadHandler extends BaseHandler {
    get pattern() {
        return "/sales/download/:type";
    }

    doGET(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登陆后再调用此API!'
            });
        } else {
            let params = JSON.parse(request.query.data),
                type = request.params.type;
            params.storeId = request.session.user.storeId;
            params.type = type;
            this.Controller.getSalesData(params, (result) => {
                var arr = [];
                if (result.status) {
                    arr = result.data;
                }
                var obj = { date: '日期', count: '数量' },
                    mapList = {
                        amount: { date: '日期', totalamount: '销售额' },
                        rate: { date: '日期', jointrate: '连带率' },
                        price: { date: '日期', guestprice: '客单价' },
                        complete: { date: '日期', ratio: '成交率' },
                        count: { date: '日期', count: '成交单数' },

                    }, nameList = {
                        amount: 'dashboard-amount.csv',
                        rate: 'dashboard-rate.csv',
                        price: 'dashboard-price.csv',
                        complete: 'dashboard-ratio.csv',
                        count: 'dashboard-count.csv',
                        
                    }, name = '';
                obj = mapList[type];
                name = nameList[type];
                arr.unshift(obj);

                utils.exportCSV(request, response, name, arr);
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }


}

module.exports = SalesDownloadHandler;